<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts';
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    interval: {
      type: Number,
      default: 0,
    },
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    },
    datetimes: {
      type: Array,
      required: true
    },
    powerMax: {
      type: Object,
      required: true
    },
    powerUnit: {
      type: Array,
      required: true
    },
    powerColor: {
      type: String
    },
    powerType: {
      type: String
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions({ expectedData, dataV } = {}) {
      this.chart.setOption({
          xAxis: {
            data: this.datetimes,
            boundaryGap: false,
            axisTick: {
              show: false
            },
            nameTextStyle: {fontStyle: 'oblique'},
            axisLabel: {
              showMaxLabel: true,
              interval: this.interval,
              rotate: 40//角度顺时针计算的
            }
          },
          grid: {
            left: 10,
            right: '2%',
            bottom: 20,
            top: 30,
            containLabel: true
          },
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'cross'
            },
            padding: [5, 10],
            /*formatter: '{b0}<br />' + this.powerType + '：{c0}<br />{c1}<br />{c2}',*/
          },
          yAxis: [
            {
              type: 'value',
              name: '总注册数',
              axisTick: {
                show: false
              },
              //max: this.powerMax.maxW,
              axisLine: {
                show: expectedData.length > 0 ? true : false,
                lineStyle: {
                }
              },
              axisLabel: {
                formatter: '{value}'
              },
            },
            {
              type: 'value',
              offset: 1,
              name: '新注册数',
              position: 'right',
              alignTicks: {
                show: false
              },
              axisLine: {
                show: dataV.length > 0 ? true : false,
                lineStyle: {
                }
              },
              //max: this.powerMax.maxV,
              axisLabel: {
                formatter: '{value}'
              }
            }
          ],
          legend: {
            data: ['总注册数', '新注册数' ],
            show: true
          },
          series: [
            {
              name: '总注册数', itemStyle: {
                normal: {
                  color: this.powerColor,
                  lineStyle: {
                    color: this.powerColor,
                    width: 2
                  }
                }
              },
              colorBy: 'series',
              smooth: true,
              type: 'line',
              symbol: 'circle',
              symbolSize: 5,
              label: {
                show: false,
                position: 'left'
              },
              data: expectedData,
              animationDuration: 2800,
              animationEasing: 'cubicInOut'
            },
            {
              name: '新注册数',
              type: 'bar',
              barWidth: 10,
              symbol: 'circle',
              symbolSize: 5,
              yAxisIndex: 1,
              data: dataV,
              animationDuration: 2800,
              animationEasing: 'cubicInOut'
            },
          ]
        })

    }
  }
}
</script>
